package com.juejin.www.dao;

import com.juejin.www.model.DO.Comment;
import com.juejin.www.model.DTO.CommentDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Jacky
 */
@Mapper
public interface CommentDao {
    /**
     * 评论文章
     * @param comment 评论实体
     */
    @Insert("insert into comments(id, be_commented_id, be_commented_type, be_commented_item,be_commented_item_type,author_id, content, like_count, reply_count, create_time, update_time) " +
            "values(null,#{beCommentedId},#{beCommentedType},#{beCommentedItem},#{beCommentedItemType},#{authorId},#{content},#{likeCount},#{replyCount},#{createTime},#{updateTime})")
    void add(Comment comment);

    /**
     * 添加文章评论数
     * @param id 文章id
     */
    @Update("update articles set comment_count = comment_count+1 where id = #{id}")
    void addArticleCommentCount(Integer id);

    /**
     * 增加沸点评论数
     * @param id 沸点id
     */
    @Update("update boiling_points set comment_count = comment_count+1 where id=#{id}")
    void addBoilingPointCommentCount(Integer id);

    /**
     * 增加评论回复数
     * @param id 沸点id
     */
    @Update("update comments set reply_count = reply_count+1 where id=#{id}")
    void addCommentReplyCount(Integer id);

    /**
     * 获取评论
     * @param beCommentedId 被评论id
     * @param beCommentedType 被评论类型
     * @return 评论
     */
    @Select("select * from comments where be_commented_id=#{beCommentedId} and be_commented_type=#{beCommentedType} order by create_time desc")
    List<CommentDTO> getComments(Integer beCommentedId, Integer beCommentedType);

    /**
     * 删除评论
     * @param id 评论id
     */
    @Delete("delete from comments where id=#{id}")
    void deleteComment(Integer id);

    /**
     * 获取被评论对象
     * @param id 评论id
     * @return 被评论对象
     */
    @Select("select * from comments where id = #{id}")
    CommentDTO getBeCommented(Integer id);

    /**
     * 当文章或沸点删除时，删除所属评论
     * @param beCommentedItem 被评论对象
     * @param beCommentedItemType 被评论对象类型
     */
    @Delete("delete from comments where be_commented_item=#{beCommentedItem} and be_commented_item_type=#{beCommentedItemType}")
    void deleteCommentWhenDeleteItem(Integer beCommentedItem,Integer beCommentedItemType);
}
